﻿// -----------------------------------------------------------------------
// <copyright file="NodeTypeData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db NodeType
    /// </summary>
    public class NodeTypeData
    {
        /// <summary>
        /// Get NodeType By Id
        /// </summary>
        /// <param name="nodeTypeId">Param nodeTypeId</param>
        /// <returns>Return NodeType</returns>
        public NodeTypeEN GetNodeTypeById(int nodeTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeTypeEN nodeType = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeTypeGetById, nodeTypeId);
                while (reader.Read())
                {
                    nodeType = new NodeTypeEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeType.NodeTypeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeType.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeType.Name = reader.GetString(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return nodeType;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all NodeType
        /// </summary>
        /// <returns>Return list all NodeType</returns>
        public IList<NodeTypeEN> GetNodeTypes()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeTypeEN> lstNodeType = new List<NodeTypeEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeTypeGetAll);
                while (reader.Read())
                {
                    NodeTypeEN nodeType = new NodeTypeEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeType.NodeTypeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeType.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeType.Name = reader.GetString(2);
                    }

                    lstNodeType.Add(nodeType);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeType;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db NodeType
        /// </summary>
        /// <param name="nodeType">Paarm NodeType</param>
        /// <returns>Insert NodeType</returns>
        public int Insert(NodeTypeEN nodeType)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeType.Name, nodeType.Name),
                                    new SqlParameter(Constants.SqlParamerterName.NodeType.Code, nodeType.Code)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeTypeInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db NodeType
        /// </summary>
        /// <param name="nodeType">Param NodeType</param>
        public void Update(NodeTypeEN nodeType)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeType.NodeTypeId, nodeType.NodeTypeId),
                                   new SqlParameter(Constants.SqlParamerterName.NodeType.Name, nodeType.Name),
                                    new SqlParameter(Constants.SqlParamerterName.NodeType.Code, nodeType.Code)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeTypeUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete NodeType by nodeTypeId
        /// </summary>
        /// <param name="nodeTypeId">param nodeTypeId</param>
        public void Delete(int nodeTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeType.NodeTypeId, nodeTypeId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeTypeDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}